﻿using System;
using System.Text;

namespace Matrix
{
    class Matrix
    {
        //Here we contain the user input number
        static int number = 4;
        //String that we pass in Console.Write() e.g "{0,4}" 
        static string numberAlignmentString;

        //Method that calculate our digit alignment
        //First, we get the length of the number * number
        //Second, we make the string e.g "{0,2}"
        static string SetNumberAlignment()
        {
            StringBuilder str = new StringBuilder();
            str.Append("{0,")
               .Append((number * number).ToString().Length + 1)
               .Append("}");
            return str.ToString();
        }

        //Method that print the matrix
        static void PrintMatrix(int[,] matrix)
        {
            for (int row = 0; row < number; row++)
            {
                for (int col = 0; col < number; col++)
                {
                    Console.Write(numberAlignmentString, matrix[row, col]);
                }
                Console.WriteLine();
            }
        }

        static void MatrixA()
        {
            //Allocate the matrix in the heap memory
            int[,] matrix = new int[number, number];
            //Our counter
            int count = 1;
            for (int col = 0; col < number; col++)
            {
                for (int row = 0; row < number; row++)
                {
                    matrix[row, col] = count++;
                }
            }
            PrintMatrix(matrix);
        }

        static void MatrixB()
        {
            int[,] matrix = new int[number, number];
            int count = 1;
            for (int i = 0; i < number; i++)
            {
                if ((i & 1) == 0)
                {
                    for (int j = 0; j < number; j++)
                    {
                        matrix[j, i] = count++;
                    }
                }
                else
                {
                    for (int j = number - 1; j >= 0; j--)
                    {
                        matrix[j, i] = count++;
                    }
                }
            }

            PrintMatrix(matrix);
        }

        static void MatrixC()
        {
            int[,] matrix = new int[number, number];

            int count = 1;

            for (int row = number - 1, col = 0; row >= 0; row--, col++)
            {
                for (int currentRow = row, currentCol = 0; currentRow < number; currentRow++, currentCol++)
                {
                    matrix[currentRow, currentCol] = count++;
                }
            }

            for (int col = 1, row = number - 2; col < number; col++, row--)
            {
                for (int currentRow = 0, currentCol = col; currentRow <= row; currentRow++, currentCol++)
                {
                    matrix[currentRow, currentCol] = count++;
                }
            }

            PrintMatrix(matrix);
        }

        static void MatrixD()
        {
            int[,] matrix = new int[number, number];
            int count = 1;
            int maxSteps = number * number;
            int currentRow = 0;
            int currentCol = 0;
            string direction = "down";

            while (count <= maxSteps)
            {
                switch (direction)
                {
                    case "down":
                        while (true)
                        {
                            if (currentRow < number)
                            {
                                if (matrix[currentRow, currentCol] == 0)
                                {
                                    matrix[currentRow++, currentCol] = count++;
                                    continue;
                                }
                            }
                            direction = "right";
                            currentRow--;
                            currentCol++;
                            break;
                        }
                        break;
                    case "right":
                        while (true)
                        {
                            if (currentCol < number)
                            {
                                if (matrix[currentRow, currentCol] == 0)
                                {
                                    matrix[currentRow, currentCol++] = count++;
                                    continue;
                                }
                            }

                            direction = "up";
                            currentCol--;
                            currentRow--;
                            break;
                        }
                        break;
                    case "up":
                        while (true)
                        {
                            if (currentRow >= 0)
                            {
                                if (matrix[currentRow, currentCol] == 0)
                                {
                                    matrix[currentRow--, currentCol] = count++;
                                    continue;
                                }
                            }

                            direction = "left";
                            currentRow++;
                            currentCol--;
                            break;
                        }
                        break;
                    case "left":
                        while (true)
                        {
                            if (currentCol > 0)
                            {
                                if (matrix[currentRow, currentCol] == 0)
                                {
                                    matrix[currentRow, currentCol--] = count++;
                                    continue;
                                }
                            }

                            direction = "down";
                            currentCol++;
                            currentRow++;
                            break;
                        }
                        break;
                }
            }

            PrintMatrix(matrix);
        }

        static void Main()
        {
            Console.Write("Enter an integer: ");
            number = int.Parse(Console.ReadLine());
            numberAlignmentString = SetNumberAlignment();
            Console.WriteLine("\na->\n");
            MatrixA();
            Console.WriteLine("\n\nb->\n");
            MatrixB();
            Console.WriteLine("\n\nc->\n");
            MatrixC();
            Console.WriteLine("\n\nd->\n");
            MatrixD();
        }
    }
}